<?php
include ("simple_html_dom.php");
function tr_decode($str)
{
	//$str= str_replace(array("&nbsp;","<br>"),"",$str);
	$html = str_get_html($str);
	$paxname =array();
	$mangxx=array();
	$mangxx['pnr']=TrimStr($html->find('td.labelConfirmationNumber',0)->plaintext);
	foreach($html->find('div.sectionContent ul li strong') as $element)
				$paxname[]=strtoupper(TrimStr($element->plaintext));
	
	$mangxx['paxname']=$paxname;;
	$cb = array();
	$dem=0;
	$dem1=0;
	$idx=0;
	foreach($html->find('div.flightDisplay div.sectionContent') as $element)
		{	
			foreach($element->find('p') as $elm)
			{
				$temel =TrimStr($elm->plaintext);
				
				if (($temel!="") && ($dem1>0))
				{
				 switch($dem)
					{
						 case 0:
						 $cb[$idx]['sohieu'] =substr($temel,0,strpos($temel,"&nbsp;"));
						 $cb[$idx]['ngaybay']=date("d/m/Y",strtotime(substr($temel,-9)));
						 $dem++;	
						 break;
						 case 1:
						 $cb[$idx]['sbdi']=substr($temel,strpos($temel,"at ")+3,5)."&nbsp;".substr($temel,strpos($temel,"(")+1,3);
						 $dem++;	
						 break;
						 case 2:
						 $cb[$idx]['sbden']=substr($temel,strpos($temel,"at ")+3,5)."&nbsp;".substr($temel,strpos($temel,"(")+1,3);
						 $dem++;	
						 break;
						 default:
						
						 break;
					} 
				}
				if ($dem>2)
				{
					$dem=0;
					$idx++;
				}	
				$dem1++;
			}
			$dem=0;
			$dem1=0;
		}
	//exit;	
	$mangxx['flight']=$cb;
	$mangxx['amount']=intval($html->find('input[id="xmloutput_XTOTAL"]',0)->value);	
	return $mangxx;
}
function xyly_ngaybay($xx)
{
	$temxx=array();
	$xx->find('div', 0)->innertext='';
	$xc =TrimStr($xx->plaintext);
	$gb=substr($xc,strpos($xc,",")+2,4);
	$gb =substr($gb,0,-2).":".substr($gb,-2);
	$temxx['ngaybay']=date("d/m/Y",strtotime(substr($xc,strpos($xc,")")+2,11)));
	$temxx['sb']=$gb."&nbsp;".TrimStr(substr($xc,strpos($xc,"(")+1,3));
	
}

 function TrimStr($str)
    {
        $str        = trim($str);
        $ret_str    = "";
        for($i=0;$i < strlen($str);$i++)
        {

            if(substr($str, $i, 1) != " ")
            {

                $ret_str .= trim(substr($str, $i, 1));

            }
            else
            {
                while(substr($str,$i,1) == " ")

                {
                    $i++;
                }
                $ret_str.= " ";
                $i--; // ***
            }
        }
        return $ret_str;
    }